package com.edgclub.edg.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.activity.GzTopicDetailActivity;
import com.edgclub.edg.activity.GzWebViewActivity;
import com.edgclub.edg.adapter.EDGActivityAdapter;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.TopicApi;
import com.edgclub.edg.api.param.PostsParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.ActivityBean;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.result.EDGActivityListData;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;

/**
 * edg活动列表
 */
public class EDGActivityFragment extends GzBaseFragment {

	private boolean isLoading = false; // 是否正在加载
	private boolean isFirstLoad = true;
	private Activity mActivity;

	private int currPage = 1;
	private int pageSize = 20;
	private XRecyclerView recyclerView;
	private UserPrefUtils userPrefUtils;
	private TopicApi topicApi;
	private EDGActivityAdapter edgActivityAdapter;
	private ArrayList<ActivityBean> list;
	private TextView text_empty;
	private ImageView imageView_empty;
	private LinearLayout layout_empty;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mActivity = getActivity();
		View rootView= inflater.inflate(R.layout.fragment_edg_activity, container, false);
		return rootView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initView(view);
		initData();

	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if(isVisibleToUser && isFirstLoad){
			isFirstLoad = false;
			currPage=1;
			getActivityList(currPage);
		}else{

		}
	}

	private void initView(View view){
		initLoadView(view);
		recyclerView = (XRecyclerView) view.findViewById(R.id.recyclerview);
		LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
		layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
		recyclerView.setLayoutManager(layoutManager);
		layout_empty= (LinearLayout)view.findViewById(R.id.layout_empty);;
		text_empty= (TextView) view.findViewById(R.id.text_empty);;
		imageView_empty = (ImageView) view.findViewById(R.id.imageView_empty);
		imageView_empty.setVisibility(View.VISIBLE);
		imageView_empty.setImageResource(R.drawable.nomess);
		text_empty.setText("还没有显示相关内容哦");

		recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
			@Override
			public void onRefresh() {
				currPage = 1;
				getActivityList(currPage);
			}

			@Override
			public void onLoadMore() {
				getActivityList(currPage);
			}
		});
	}

	private void initData(){
		topicApi = new TopicApi(getActivity());
		userPrefUtils = new UserPrefUtils(getActivity());
		list = new ArrayList<>();
		edgActivityAdapter = new EDGActivityAdapter(getActivity());
		edgActivityAdapter.setList(list);
		recyclerView.setAdapter(edgActivityAdapter);

		edgActivityAdapter.setOnItemClickListener(new MyItemClickListener() {
			@Override
			public void onItemClick(View view, int postion) {
				ActivityBean data = list.get(postion);
				if(data.getType().equals("0")){
					//外部链接
					Intent intent = new Intent(mActivity, GzWebViewActivity.class);
					intent.putExtra("url",data.getProtocol());
					intent.putExtra("actbarTitle",data.getTitle());
					mActivity.startActivity(intent);
				}else if(data.getType().equals("1")){
					//内部协议 ,帖子详情
					Intent intent = new Intent(mActivity, GzTopicDetailActivity.class);
					intent.putExtra("topicId", data.getProtocol());
					mActivity.startActivity(intent);
					mActivity.overridePendingTransition(R.anim.slide_in_from_right,
							R.anim.slide_out_to_left);

				}
			}
		});
	}

	private void getActivityList(int page) {
		if (isLoading) {
			return;
		}
		User user = userPrefUtils.getUser();
		PostsParam postsParam = new PostsParam();
		postsParam.setPage(page + "");
		postsParam.setPageSize(pageSize + "");
		postsParam.setUserId(user.getId() + "");
		topicApi.getEdgActivity(postsParam, new getListListener());//edg列表
	}

	class getListListener implements VolleyListener {

		@Override
		public void onStart(String tag) {
			isLoading = true;
		}

		@Override
		public void onSuccess(String response, String tag) {

			if (JSONHelper.isSuccess(response)) {
				ArrayList<ActivityBean> activities;
				EDGActivityListData data = JsonUtil.getMode(response, EDGActivityListData.class);
				activities = data.getData();
				if(activities!=null && activities.size()>0){
					if (currPage == 1) {
						list.clear();
					}
					list.addAll(activities);
					currPage++;
					edgActivityAdapter.setList(list);
					edgActivityAdapter.notifyDataSetChanged();
				}else{
					recyclerView.noMoreLoading();
				}

			} else {

			}
			if (!"".equals(list)&&list.size()>0){
				layout_empty.setVisibility(View.GONE);
				recyclerView.setPullRefreshEnabled(true);
			}
			else {
				layout_empty.setVisibility(View.VISIBLE);
				recyclerView.setPullRefreshEnabled(false);
			}
			isLoading = false;
			recyclerView.refreshComplete();
			recyclerView.loadMoreComplete();
			setLoadViewGONE();
		}

		@Override
		public void onFaile(VolleyError volleyError, String tag) {
			isLoading = false;
			recyclerView.refreshComplete();
			recyclerView.loadMoreComplete();
			setNoNetWork(new ReLoadClickListener() {
				@Override
				public void reload() {
					getActivityList(currPage);
				}
			});
			ResultCode.toastVolleyError(getActivity(), volleyError);
		}
	}



}
